<?php
/*
**********************************************
JCal Pro v1
Copyright (c) 2006 Anything-Digital.com
**********************************************
JCal Pro v1 is a fork of the existing Extcalendar component for Joomla!
(com_jcalpro_0_9_2_RC4.zip from mamboguru.com). 
Extcal (http://sourceforge.net/projects/extcal) was renamed 
and adapted to become a Mambo/Joomla! component by 
Matthew Friedman, and further modified by David McKinnis
(mamboguru.com) to repair some security holes. 

This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* Upcoming Events Module
*
* 2006/11/13 - Version 1.0
*
* Module for displaying upcoming events in connection with the JCal Pro
* component. The component must be installed before this module will work. 
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JCal Pro at:
http://dev.anything-digital.com//
**********************************************
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$limit = $params->get('limit', 5);
$categories = $params->get('categories', '');
$months = $params->get('months', 2);
$category_link = $params->get('category_link', 0);
$item_id = intval( $params->get('item_id', -1) );
$output = intval( $params->get('output', 1) );

$cats = array();
$where = '';
if(!empty($categories)){
	$cats = explode( ',', $categories );
	for($i=0;$i<count($cats);$i++){
		$cats[$i] = 'e.cat = '.intval( $cats[$i] );
	}
}
if($months != -1){
	$where .= "\n AND e.start_date <= '" . date("Y-m-d H:i:s", strtotime( "+$months month") )."'";
}
if(count($cats) > 0){
	$where .= "\n AND ( ". implode( " OR ", $cats) . " )";
}
if($item_id == -1){
	$query = "SELECT id, menutype FROM #__menu WHERE link LIKE '%option=com_jcalpro%' AND published = 1 GROUP BY menutype";
	$database->setQuery( $query );
	$menuitems = $database->loadObjectList( 'menutype' );
	if(is_array($menuitems) && count($menuitems) > 0 ){
		if(!empty($menuitems['mainmenu'])){
			$item_id = $menuitems['mainmenu']->id;
		}
		else{
			$keys = array_keys( $menuitems );
			$item_id = $menuitems[$keys[0]]->id;
		}
	}else{
		$item_id = $Itemid;
	}
}

$query = "SELECT e.*, c.published, c.cat_name, c.color, c.bgcolor FROM #__jcalpro_events AS e LEFT JOIN #__jcalpro_categories AS c ON e.cat = c.cat_id "
	."\n WHERE e.start_date >= '".date("Y-m-d H:i:s")."'"
	.$where
	."\n ORDER BY e.start_date"
	."\n LIMIT $limit";
	
$database->setQuery( $query );
$events = $database->loadObjectList();

if(!$events || count($events) == 0){
	?>
<p>No Upcoming Events</p>
	<?php
}
else{
	switch( $output){
		case 1:
			upcoming_list( $events, $category_link, $item_id );
			break;
		case 2:
		default:
			upcoming_table( $events, $category_link, $item_id );
			break;
	}
}

function upcoming_list( $events, $category_link, $item_id ){
	?>
<ul>
	<?php
	foreach($events as $event){
		$eventLink = sefRelToAbs( 'index.php?option=com_jcalpro&amp;extmode=view&amp;extid='.$event->extid.'&amp;Itemid='.$item_id );
		$monthLink = sefRelToAbs( 'index.php?option=com_jcalpro&amp;extmode=cal&amp;date='.date('Y-m-d', strtotime( $event->start_date) ).'&amp;Itemid='.$item_id );
		$categoryLink = sefRelToAbs( 'index.php?option=com_jcalpro&amp;extmode=cat&amp;cat_id='.$event->cat .'&amp;Itemid='.$item_id);
		$diff = bib_datediff( time(), strtotime( $event->start_date ) );
		?>
		<li><?php echo $diff['days']; ?> Days: <a href='<?php echo $eventLink; ?>'><?php echo $event->title; ?></a>
		<?php
		if($category_link){
			?>
			<br /><a href="<?php echo $categoryLink; ?>" style='color: <?php echo $event->color; ?>' ><?php echo $event->cat_name; ?></a>
			<?php
		}
		?>
		</li>
		<?php
	}
	?>
</ul>	
	<?php
}
function upcoming_table( $events, $category_link, $item_id ){
	?>
	<table cellspacing='0' cellpadding='1' border='0' style='width: 98%; margin: 4px auto;'>
	<tr>
		<th style='text-align: left; border-bottom: 1px solid #333;'>Days</th>
		<th style='text-align: left; border-bottom: 1px solid #333;'>Event</th>
	<?php
	if($category_link){
		?>
		<th style='text-align: left; border-bottom: 1px solid #333;'>Category</th>
		<?php
	}
	?>
	</tr>
	<?php
	foreach($events as $event){
		?>
	<tr><td colspan='<?php echo $category_link ? '4' : '3'; ?>' style='height: 1px; line-height: 1px; font-size: 1px;'>&nbsp;</td></tr>
	<tr valign='top'>
		<?php
		$eventLink = sefRelToAbs( 'index.php?option=com_jcalpro&amp;extmode=view&amp;extid='.$event->extid.'&amp;Itemid='.$item_id );
		$monthLink = sefRelToAbs( 'index.php?option=com_jcalpro&amp;extmode=cal&amp;date='.date('Y-m-d', strtotime( $event->start_date) ).'&amp;Itemid='.$item_id );
		$categoryLink = sefRelToAbs( 'index.php?option=com_jcalpro&amp;extmode=cat&amp;cat_id='.$event->cat .'&amp;Itemid='.$item_id);
		$color = $event->color;
		$diff = bib_datediff( time(), strtotime( $event->start_date ) );
		?>
		<td style='background: #fff; text-align: left; border: 1px solid <?php echo $color; ?>; border-right-width: 0px;'><?php echo $diff['days']; ?></td>
		<td style='background: #fff; text-align: left; border: 1px solid <?php echo $color; ?>; border-left-width: 0px; <?php if(!$category_link){ ?> border-right: 1px solid #DEE7EF; <?php } else{ ?> border-right-width: 0px; <?php } ?>'><a href='<?php echo $eventLink; ?>'><?php echo $event->title; ?></a></td>
		<?php
		if($category_link){
			?>
			<td style='background: #fff; text-align: left; border: 1px solid <?php echo $color; ?>; border-left-width: 0px;'><a href="<?php echo $categoryLink; ?>"><?php echo $event->cat_name; ?></a></td>
			<?php
		}
		?>
	</tr>
		<?php
	}
	?>
</table>	
	<?php
}

function bib_datediff($fromtime, $totime=''){
       if($totime=='')        $totime = time();
      
       if($fromtime>$totime){
           $tmp = $totime;
           $totime = $fromtime;
           $fromtime = $tmp;
       }
      
       $timediff = $totime-$fromtime;
       //check for leap years in the middle
       for($i=date('Y',$fromtime); $i<=date('Y',$totime); $i++){
           if ((($i%4 == 0) && ($i%100 != 0)) || ($i%400 == 0)) {
               $timediff -= 24*60*60;
           }
       }
       $remain = $timediff;
       $ret['years']    = intval($remain/(365*24*60*60));
       $remain            = $remain%(365*24*60*60);
       $ret['days']    = intval($remain/(24*60*60));
       $remain            = $remain%(24*60*60);

       $m[0]    = 31;        $m[1]    = 28;        $m[2]    = 31;        $m[3]    = 30;
       $m[4]    = 31;        $m[5]    = 30;        $m[6]    = 31;        $m[7]    = 31;
       $m[8]    = 30;        $m[9]    = 31;        $m[10]    = 30;        $m[11]    = 31;
       //if leap year, february has 29 days
       if (((date('Y',$totime)%4 == 0) && (date('Y',$totime)%100 != 0)) || (date('Y',$totime)%400 == 0)){

           $m[1] = 29;
       }
       $ret['months']        = 0;
       foreach($m as $value){
           if($ret['days']>$value){
               $ret['months']++;
               $ret['days'] -=$value;
           } else {
               break;
           }
       }
       $ret['hours']    = intval($remain/(60*60));
       $remain            = $remain%(60*60);
       $ret['minutes']    = intval($remain/60);
       $ret['seconds']    = $remain%60;
       return $ret;
   }

?>